package com.bjyhl.app.push;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.bjyhl.app.AppManager;
import com.bjyhl.core.util.device.Device;
import com.bjyhl.core.util.log.LogUtils;
import com.bjyhl.app.MainActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.Set;

import cn.jpush.android.api.JPushInterface;

public class PushReceiver extends BroadcastReceiver {
    private final String TAG="jpush";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            String message= bundle.getString(JPushInterface.EXTRA_MESSAGE);
            if (message!=null&&!TextUtils.isEmpty(message)){
                switch (message){
                    case "重启":
                        Device.reboot();
                        break;
                    case "升级":
                        // 通过设备管理端自动升级
//                        Toast.makeText(context, "应用即将自动升级，给您造成的不便敬请谅解！",Toast.LENGTH_LONG).show();
//                        Utils.updateApk(context);
                        break;
                    case "开启屏保广告":
                        SharedPreferences preferences= context.getSharedPreferences("pingBaoGuangGao",Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor= preferences.edit();
                        editor.putBoolean("show",true);
                        editor.commit();
                        break;
                    case "关闭屏保广告":
                        SharedPreferences preferences1= context.getSharedPreferences("pingBaoGuangGao",Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor1= preferences1.edit();
                        editor1.putBoolean("show",false);
                        editor1.commit();
                        break;
                    case "MAC地址上传":
                        // 通过心跳上传
//                        Utils.updateMacAddress(YLTApplication.getContext(),false);
                        break;
                    case "更新患者数据":
                        // TODO
                        break;
                    case "切换爱奇艺":
                        // 已停用
//                        SharedPreferences sharedPreferences =   YLTApplication.getContext().getSharedPreferences("videoType",YLTApplication.getContext().MODE_PRIVATE);
//                        SharedPreferences.Editor editor2= sharedPreferences.edit();
//                        editor2.putBoolean("isiqiyi",true);
//                        editor2.commit();
//                        AppManager.getAppManager().AppExit(YLTApplication.getContext());
                        break;
                    case "切换切片":
                        // 已停用
//                        SharedPreferences sharedPreferences1 =   YLTApplication.getContext().getSharedPreferences("videoType",YLTApplication.getContext().MODE_PRIVATE);
//                        SharedPreferences.Editor edit= sharedPreferences1.edit();
//                        edit.putBoolean("isiqiyi",false);
//                        edit.commit();
//                        AppManager.getAppManager().AppExit(YLTApplication.getContext());
                        break;

                    case "切换护士":
                        // TODO
//                        YLTApplication.getContext().getSharedPreferences("devicebe",YLTApplication.getContext().MODE_PRIVATE).edit().putString("belong","护士").commit();
//                        AppManager.getAppManager().AppExit(YLTApplication.getContext());
                        break;
                    case "切换科室":
                        // TODO
//                        YLTApplication.getContext().getSharedPreferences("devicebe",YLTApplication.getContext().MODE_PRIVATE).edit().putString("belong","科室").commit();
//                        AppManager.getAppManager().AppExit(YLTApplication.getContext());
                        break;
                    case "切换患者":
                        // TODO
//                        YLTApplication.getContext().getSharedPreferences("devicebe",YLTApplication.getContext().MODE_PRIVATE).edit().putString("belong","患者").commit();
//                        AppManager.getAppManager().AppExit(YLTApplication.getContext());
                        break;
                    case "删除设备":
                        // 已停用
//                        try {
//                            MovieSdk.getInstance().sdk_deviceDelete(new OnResponseListener() {
//                                @Override
//                                public void onResponse(HttpResult result) {
//                                    if (result.code==200){
//                                        Toast.makeText(YLTApplication.getContext(),"解绑成功",Toast.LENGTH_SHORT).show();
//                                    }else {
//                                        Toast.makeText(YLTApplication.getContext(),"解绑失败",Toast.LENGTH_LONG).show();
//                                    }
//                                }
//                            });
//                        }catch (Exception e){
//                            e.printStackTrace();
//                        }
                        break;
                    case "关闭设置":
                        //不实用
//                        SharedPreferences preferencesSet= context.getSharedPreferences("openSet",Context.MODE_PRIVATE);
//                        SharedPreferences.Editor editorSet= preferencesSet.edit();
//                        editorSet.putBoolean("open",false);
//                        editorSet.commit();
                        break;
                    case "开启设置":
                        //不实用
//                        SharedPreferences preferencesSetPreferences2= context.getSharedPreferences("openSet",Context.MODE_PRIVATE);
//                        SharedPreferences.Editor editorSetEditor2= preferencesSetPreferences2.edit();
//                        editorSetEditor2.putBoolean("open",true);
//                        editorSetEditor2.commit();
                        break;
                    case "刷新广告新闻":
                        //已停用
//                        EventBus.getDefault().post(new DataEvent(DataEvent.Type.RELOAD_GUANGGAO_NEWS, "", true));
                        break;
                    case "重启应用":
                        AppManager.getAppManager().AppExit();
                        break;
                    case "开启电视剧":
                        //不实用
//                        SharedPreferences sharedPreferences= context.getSharedPreferences("openTV",Context.MODE_PRIVATE);
//                        SharedPreferences.Editor edit= sharedPreferences.edit();
//                        edit.putBoolean("open",true);
//                        edit.commit();
//                        EventBus.getDefault().post(new DataEvent(DataEvent.Type.RELOAD_DIANSHIJU, "", true));
                        break;
                    default:
                        //使用设备管理升级
//                        if (message.indexOf("外网升级")!=-1){
//                            Utils.downLoadApk(message.replace("外网升级",""));
//                        }
//                        if (message.indexOf("外网安装")!=-1){
//                            Utils.downLoadApk(message.replace("外网安装",""));
//                        }
//                        if (message.indexOf("更换密码")!=-1){
//                            SharedPreferences prePreferences3=context.getSharedPreferences("lockPassword",Context.MODE_PRIVATE);
//                            SharedPreferences.Editor edit1= prePreferences3.edit();
//                            edit1.putString("password",message.replace("更换密码",""));
//                            edit1.commit();
//                        }
//                        if (message.indexOf("付费评分")!=-1){
//                            try {
//                                SharedPreferences coreRorCharge=YLTApplication.getContext().getSharedPreferences("chargeCore",Context.MODE_PRIVATE);
//                                SharedPreferences.Editor coreRorChargeEditor3=coreRorCharge.edit();
//                                coreRorChargeEditor3.putFloat("core",Float.valueOf(message.replace("付费评分","")));
//                                coreRorChargeEditor3.commit();
//                            }catch (Exception e){
//                                e.printStackTrace();
//                            }
//                        }
//                        if (message.indexOf("首页广告")!=-1){
//                            SharedPreferences prePreferences3=context.getSharedPreferences("ShouYeGuangGao",Context.MODE_PRIVATE);
//                            SharedPreferences.Editor editor3= prePreferences3.edit();
//                            editor3.putString("guanggao",message.replace("首页广告",""));
//                            editor3.commit();
//                            EventBus.getDefault().post(new DataEvent(DataEvent.Type.RELOAD_GUANGGAO_NEWS, "", true));
//                        }
                        break;
                }
            }
            // 自定义消息不会展示在通知栏，完全要开发者写代码去处理
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            String message= bundle.getString(JPushInterface.EXTRA_EXTRA,"");
            String title= bundle.getString(JPushInterface.EXTRA_ALERT,"");
            if (message!=null&&!TextUtils.isEmpty(message)){

//                try {
//                    org.json.JSONObject jsonObjec= new org.json.JSONObject(message);
//                    String status  =jsonObjec.optString("status","");
//                    if (TextUtils.isEmpty(status))
//                    {
//                        return;
//                    }
//                    if (status.equals("0")){
//                        Intent intent1= new Intent(YLTApplication.getContext(), NurseNoticeActivity.class);
//                        intent1.putExtra("message",title);
//                        intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                        YLTApplication.getContext().startActivity(intent1);
//                        return;
//                    }
//                    if (status.equals("80")){
//                        String userinfo=jsonObjec.optString("userinfo","");
//                        try {
//
//                            UserManager.getUserManager().saveWXLoginInfo(userinfo.replaceAll("\\\\",""));
//                            EventBus.getDefault().post(new DataEvent(DataEvent.Type.WX_SCAN_LOGIN, "", true));
//                            return;
//                        }catch (Exception e){
//                            e.printStackTrace();
//                            return;
//                        }
//                    }
//
//                    if (status.equals("99")){
//                        String userinfo=jsonObjec.optString("payment","");
//                        EventBus.getDefault().post(new DataEvent(DataEvent.Type.MOVIE_CHAGE_SUCCESS, "", userinfo));
//                        return;
//                    }
//                    if (status.equals("66")){
//                        EventBus.getDefault().post(new DataEvent(DataEvent.Type.PATIENT_YIZHU, "", true));
//                        return;
//                    }
//                    if (status.equals("77")){
//                        EventBus.getDefault().post(new DataEvent(DataEvent.Type.REJECT_ORDER, "", true));
//                        return;
//                    }
//                    String mac  =jsonObjec.optString("mac","");
//                    String macaddress  =jsonObjec.optString("macaddress","");
//                    if (TextUtils.isEmpty(mac)){
//                        String patient_mobile  =jsonObjec.optString("patient_mobile","");
//                        String family_mobile  =jsonObjec.optString("family_mobile","");
//                        switch (status){
//                            case "1":
//                                Intent intent1= new Intent(YLTApplication.getContext(), OnFamilyCallInActivity.class);
//                                intent1.putExtra("title",title);
//                                intent1.putExtra("MyMac",patient_mobile);
//                                intent1.putExtra("OtherMac",family_mobile);
//                                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                                YLTApplication.getContext().startActivity(intent1);
//                                break;
////                            case "2":
////                                Toast.makeText(YLTApplication.getContext(),"对方拒绝了您的视频邀请",Toast.LENGTH_SHORT).show();
////                                EventBus.getDefault().post(new DataEvent(DataEvent.Type.CALL_TYPE, "", false));
////                                EventBus.getDefault().post(new DataEvent(DataEvent.Type.OTHER_NO_ANWSER, "", true));
////                                break;
//                            case "3":
//                                Intent intent2=  new Intent(YLTApplication.getContext(), SmartEchoCancelWithFamilyActivity.class);
//                                intent2.putExtra("MyMac",patient_mobile);
//                                intent2.putExtra("OtherMac",family_mobile);
//                                intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                                YLTApplication.getContext().startActivity(intent2);
//                                EventBus.getDefault().post(new DataEvent(DataEvent.Type.OTHER_NO_ANWSER, "", true));
//                                break;
////                            case "4":
////                                EventBus.getDefault().post(new DataEvent(DataEvent.Type.OTHER_CLOSE_CALL, "", true));
////                                break;
//                            case "5":
//                                EventBus.getDefault().post(new DataEvent(DataEvent.Type.OTHER_CLOSE_CALL, "", true));
//                                break;
//                        }
//
//                    }else {
//                        switch (status){
//                            case "1":
//                                Intent intent1= new Intent(YLTApplication.getContext(), OnCallInActivity.class);
//                                intent1.putExtra("title",title);
//                                intent1.putExtra("MyMac",macaddress);
//                                intent1.putExtra("OtherMac",mac);
//                                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                                YLTApplication.getContext().startActivity(intent1);
//                                break;
//                            case "2":
//                                Toast.makeText(YLTApplication.getContext(),"对方已收到您的呼叫",Toast.LENGTH_SHORT).show(); // 对方拒绝了您的视频邀请
//                                EventBus.getDefault().post(new DataEvent(DataEvent.Type.CALL_TYPE, "", false));
//                                EventBus.getDefault().post(new DataEvent(DataEvent.Type.OTHER_NO_ANWSER, "", true));
//                                break;
//                            case "3":
//                                Intent intent2=  new Intent(YLTApplication.getContext(), SmartEchoCancelActivity.class);
//                                intent2.putExtra("MyMac",macaddress);
//                                intent2.putExtra("OtherMac",mac);
//                                intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                                YLTApplication.getContext().startActivity(intent2);
//                                EventBus.getDefault().post(new DataEvent(DataEvent.Type.OTHER_NO_ANWSER, "", true));
//                                break;
//                            case "4":
//                                EventBus.getDefault().post(new DataEvent(DataEvent.Type.OTHER_CLOSE_CALL, "", true));
//                                break;
//                        }
//
//
//                    }
//
//                }catch (Exception e){
//                    e.printStackTrace();
//                }

            }

        }
    }

//    //广播接收后 执行的回调
//    @Override
//    public void onReceive(Context context, Intent intent) {
//        final Bundle bundle = intent.getExtras();
//        final Set<String> keys = bundle.keySet();
//        final JSONObject json = new JSONObject();
//        for (String key : keys) {
//            final Object val = bundle.get(key);
//            json.put(key, val);
//        }
//
//        LogUtils.json("PushReceiver", json.toJSONString());
//
//        final String pushAction = intent.getAction();
//
//        if (pushAction.equals(JPushInterface.ACTION_NOTIFICATION_RECEIVED)) {
//            //处理接收到的消息类型的推送
//            onReceivedMessage(bundle);
//
//        } else if (pushAction.equals(JPushInterface.ACTION_NOTIFICATION_OPENED)) {
//            //打开相应的Notification类型的通知推送
//            onOpenNotification(context, bundle);
//        }
//    }
//
//    private void onReceivedMessage(Bundle bundle) {
//        final String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
//        final String msgId = bundle.getString(JPushInterface.EXTRA_MSG_ID);
//        final int notificationId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
//        final String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
//        final String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
//        final String alert = bundle.getString(JPushInterface.EXTRA_ALERT);
//    }
//
//    private void onOpenNotification(Context context, Bundle bundle) {
//        final String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
//        final Bundle openActivityBundle = new Bundle();
//        final Intent intent = new Intent(context, MainActivity.class);
//        intent.putExtras(openActivityBundle);
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        ContextCompat.startActivity(context, intent, null);
//    }

}
